<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/20
 * Time: 上午11:55
 */

class PubDateRange {
    // 根据区间分解出开始日期
    public static function getStartTime($dateRange){
        return date('Y-m-d', strtotime(str_replace('.', '', substr($dateRange, 0, 10))));
    }

    // 根据区间分解出结束日期
    public static function getEndTime($dateRange){
        return date('Y-m-d', strtotime(str_replace('.', '', substr($dateRange, 13, 10))));
    }

    // 获得区间的值
    public static function getRange($begin, $end){
        return "{$begin} ~ {$end}";
    }

    // 获得今天的区间值
    public static function getTodayRange(){
        $today = date('Y.m.d');
        return self::getRange($today, $today);
    }

    // 获得昨天的区间值
    public static function getYesterdayRange(){
        $today = date('Y.m.d', strtotime('yesterday'));
        return self::getRange($today, $today);
    }

    // 获得过去七天的区间值
    public static function getLastDayRange($day, $time=false, $minTime=false){
        if($time == false) {
            $time = time();
        }

        $end    = date('Y.m.d', $time);

        $begin  = date('Y.m.d', strtotime("- {$day} days", $time));
        if($minTime !== false && strtotime(str_replace('.', '-', $begin)) < $minTime){
            $begin = date('Y.m.d', $minTime);
        }

        return self::getRange($begin, $end);
    }

} 